﻿Imports System.Data.OleDb
Imports System.Data.Common
Imports Microsoft.VisualBasic.FileIO

Public Class qDbManager
    Public qidToLoad As Integer 'this will hold the QID (unique query ID) for user to load a scecific query from DB


    Private Sub Form2_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
        logdbConnection() 'call logDBConnection Function
    End Sub

    Private Sub logdbConnection()
        Dim statement As String
        statement = "SELECT * FROM RecFinder_Log"    'This is a variable that stores a sql Statement with the table we selected from tableList ListBox"
        Dim conn As New OleDbConnection

        If My.Settings.RegistryAccessible = True Then 'If Registry is operational '<-- FIX 3/1/2012
            conn.ConnectionString = "Provider=Microsoft.Jet.OLEDB.4.0;Data Source=" & My.Computer.Registry.GetValue("HKEY_LOCAL_MACHINE\Software\RecFinder\recfinder_connString", "path", Nothing) 'passing the path to logdb from registry
            qdbPath.Text = "Reading from : Registry"
        Else
            conn.ConnectionString = "Provider=Microsoft.Jet.OLEDB.4.0;Data Source=" & Main.defaultPath 'If Registry is not functional, use the default path
            qdbPath.Text = "Reading from : Settings Default Path"
        End If

        Try

            Using adapter As New OleDbDataAdapter(statement, conn.ConnectionString) 'This adapter will be used for the interaction with Database
                Dim dst As New DataSet  'This dataset will do most of the job

                adapter.Fill(dst, "RecFinder_Log") 'Dataset fills with the table we give

                logView.DataSource = dst.Tables(0)  'The Datagrid takes the table and shows it

                If logView.Rows(0).Cells(0).Value = Nothing Then 'If Datagrid first row and first cell is empty, meaning that db has now records
                    btnLoad.Enabled = False 'hide button load
                    btnDelete.Enabled = False 'and so button delete
                End If

            End Using
        Catch ex As Exception 'If Database cannot be found, for example because Registry is not holding the right path 
            My.Settings.RegistryAccessible = False 'Bypass Registry if any error occurs, and use My.Settings.ConnectionString
            Main.defaultPath = My.Settings.defaultLogPath ' <--- FIX 3/1/2013 , restore default path to app from settings
            MsgBox("Database not found, Using Default Path (C:\RecFinder_Databases\UserQueryDB\rec_finder_logs.mdb)" & vbNewLine & "Please Close this Query Manage Form and Re-Open it", MsgBoxStyle.Exclamation) 'notify user <--FIX 3/1/2013 Proper message now shows up

        End Try
    End Sub

    Private Sub Button3_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btOK.Click
        LoadDialog.Close() 'Close dialog load if left open
        DeleteQueryDialog.Close() 'Close Dialog Delete if left open
        Me.Close() 'close form
    End Sub

    Private Sub btnLoad_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnLoad.Click
        LoadDialog.Show() 'show load dialog
    End Sub

    Private Sub logView_CellClick(ByVal sender As System.Object, ByVal e As System.Windows.Forms.DataGridViewCellEventArgs) Handles logView.CellClick
        Try
            MsgBox(logView.Rows(e.RowIndex).Cells(e.ColumnIndex).Value) 'Show every cell user clicks on
        Catch ex As Exception
            MsgBox("Cell is empty", MsgBoxStyle.Exclamation) 'if user clicks on empty cell, exception will raise, but will be caught
        End Try

    End Sub

    Private Sub btnDelete_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnDelete.Click
        DeleteQueryDialog.Show() 'Show the Delete Query Dialog
    End Sub
End Class